import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;
import java.text.*;
import javax.swing.*;

import java.util.*;

//test

@SuppressWarnings("serial")
public class ChatArea extends JPanel implements MouseListener, ActionListener
{
	CommandLine command;
	ChatPanel chatPanel;
	ChatPopup popup;
	DateFormat format;
	DefaultListModel messages, dates;
	JList list;
	String date, msg;
	
	public ChatArea(ChatPanel panel)
	{
		setLayout(new BorderLayout());
		
		chatPanel = panel;
		
		format = new SimpleDateFormat("[HH:mm:ss] ");

		dates = new DefaultListModel();
		JList dateList = new JList();
		dateList.setModel(dates);
		dateList.setMinimumSize(new Dimension(100, 0));
		dateList.setSelectionBackground(null);
		dateList.setFocusable(false);
		add(dateList,BorderLayout.WEST);
		
		messages = new DefaultListModel();
		
		list = new JList();
		list.setFont(getFont().deriveFont(Font.PLAIN));
		list.setModel(messages);
		list.setMinimumSize(new Dimension(400, 0));
		add(list,BorderLayout.CENTER);
		list.addMouseListener(this);
		
		popup = new ChatPopup(this);
	}
	
	public void welcomeMessage()
	{
		add(format.format(new Date()), "<html><b><font color=\"green\">Welcome to the Hendrix Comp Sci Chat Room</font></b></html>");
	}
	
	public void login(String username)
	{
		add("<html><font color=\"#FF8040\">" + username + " has logged in.</font></html>");
	}
	
	public void logoff(String username)
	{
		add("<html><font color=\"#FF8040\">" + username + " has logged out.</font></html>");
	}

	public void listUsers(UserList users) 
	{
		String list = "<html>Active Users: <b><font color=\"red\">";
		ListModel model = users.getModel();
		if(model.getSize() > 0)
			list += model.getElementAt(0);
		for(int i = 1; i < model.getSize(); i++)
		{
			String name = model.getElementAt(i).toString();
			if(name.length() > 0)
				list += ", " + name;
		}
		add(list + ".</font></b></html>");
	}
	
	public void addText(String username, String text)
	{
		add("<html>&lt;" + username + "&gt; " + filterInput(text) + "</html>");
	}

	public void addTxt(String text) { 
		add("<html>" + filterInput(text) + "</html>");
	}
	
	public void addWhisper(String username, String text)
	{
		add("<html>From &lt;" + username + "&gt; " + "<font color=\"blue\">: " + filterInput(text) + "</font></html>");
	}


	public void sendWhisper(String username, String text)
	{
		add("<html>To &lt;" + username + "&gt; " + "<font color=\"blue\">: " + filterInput(text) + "</font></html>");
	}
	
	public void poke(String username)
	{
		add("<html><font color=\"#FF8040\">You poked " + username + ".</font></html>");
	}
	
	public void poked(String username)
	{
		add("<html><font color=\"#FF8040\">You were poked by " + username + ".</font></html>");
	}
	
	public void ignore(String username)
	{
		add("<html><font color=\"#FF8040\">You are now ignoring " + username + ".</font></html>");
	}
	
	public void listen(String username)
	{
		add("<html><font color=\"#FF8040\">You have stopped ignoring " + username + ".</font></html>");
	}

	public void addSystemText(String text) 
	{
		add("<html><font color=\"#FF8040\">" + text + "</font></html>");
	}
	
	private void add(String text)
	{
		add(format.format(new Date()), text);
	}

	private synchronized void add(String time, String text) {
		if(!SwingUtilities.isEventDispatchThread())
		{
			date = time;
			msg = text;
				SwingUtilities.invokeLater(new Runnable()
				{
					@Override
					public void run() 
					{
						addText(dates,date);
						addText(messages,msg);
					}
				});
		} else
		{
			addText(dates,time);
			addText(messages,text);
		}
	}
	
	private void addText(DefaultListModel text, String str)
	{
		text.addElement(str);
	}

	@Override
	public void mouseClicked(MouseEvent e) 
	{
	}

	@Override
	public void mouseEntered(MouseEvent e) {}

	@Override
	public void mouseExited(MouseEvent e) {}

	@Override
	public void mousePressed(MouseEvent e) 
	{
	}

	@Override
	public void mouseReleased(MouseEvent e) 
	{
		if(e.getButton() == MouseEvent.BUTTON3)
		{
			displayPopup(e);
		}
	}
	
	public void displayPopup(MouseEvent e)
	{
		int loc = list.locationToIndex(e.getPoint());
		list.setSelectedIndex(loc);
		String username = getUsername(list.getSelectedValue().toString());
		if(username.length() > 0 && !username.equals(chatPanel.username))
		{
			if(chatPanel.isIgnoring(username))
				popup.showIgnore();
			else
				popup.showListen();
			popup.show(e.getComponent(), e.getX(), e.getY());
		}
	}

	
	
	public String getUsername(String message)
	{
		String[] arr = message.split("(<|>|&gt;|&lt;)");
		if(arr.length >= 3)
		{
			if(!arr[1].equals("html"))
				return arr[1];
			if(arr[2].indexOf("&lt;") != -1)
				return arr[2].replaceAll("&..;", "");
		}
		return "";
	}
	
	public String filterInput(String text)
	{
		return text.replace("<", "&lt;").replace(">", "&gt;");
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		String username = getUsername(list.getSelectedValue().toString());
		if(e.getActionCommand().equalsIgnoreCase("poke"))
		{
			chatPanel.sendPoke(username);
		} else if(e.getActionCommand().equalsIgnoreCase("whisper"))
		{
			chatPanel.sendWhisper(username);
		} else if(e.getActionCommand().equalsIgnoreCase("ignore"))
		{
			chatPanel.sendIgnore(username);
		} else if(e.getActionCommand().equalsIgnoreCase("listen"))
		{
			chatPanel.cancelIgnore(username);
		} else
		{
			chatPanel.game.startGame(username);
		}
	}
}
